package com.rockfalldefence.sw.embark.client.page;

import net.binarymuse.gwt.client.ui.wizard.WizardPage;
import net.binarymuse.gwt.client.ui.wizard.event.NavigationEvent;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.rockfalldefence.sw.embark.client.RDEmbarkContext;

public class TraiettoriaPage extends WizardPage<RDEmbarkContext> {

	public static final PageID PAGEID = new PageID();
	protected HorizontalPanel hPanel = new HorizontalPanel();
	protected FlowPanel panel;
	protected FlexTable t1;
	protected TextBox tbBeta; 
	protected TextBox tbVt;  
	protected TextBox tbMt;    
	protected TextBox tbVolt; 
	protected TextBox tbEt; 
	protected TextBox tbRhot; 
	protected TextBox tbOmegat; 
	protected TextBox tbPhit; 
	protected double diameter = 0;
	protected double energy = 0;
	protected double speed = 0;
	protected double omega = 0;
	protected double volume = 0;
	protected double rho = 0;
	protected double alpha = 0;
	protected double mass = 0;
	
	
	@Override
	public void beforeNext(NavigationEvent event) {
		super.beforeNext(event);
		RDEmbarkContext rdcntx = getContext();
		rdcntx.setBetat(tbBeta.getText());
		rdcntx.setVt(tbVt.getText());
		rdcntx.setOmegat(tbOmegat.getText());
		rdcntx.setVt(tbVt.getText());
		rdcntx.setPhit(tbPhit.getText());
		rdcntx.setE(tbEt.getText());
		rdcntx.setMt(tbMt.getText());
	}

	public TraiettoriaPage() {
		panel = new FlowPanel();
	    panel.add(new HTML("<h1>Traiettoria del Blocco</h1>"));
	    panel.add(new HTML("<p>Scegli i parametri " + 
	    		"necessari per la definizione della traiettoria del blocco.</p>"));
	    
	    hPanel = new HorizontalPanel();
	    t1 = new FlexTable();
	    
	    Image img = new Image("images/trapezio_small.png");
	    t1.setWidget(0, 0, img);
	    Label lb = new Label("Energia del blocco (kJ)");
	    lb.setTitle("Energia");	    
	    tbEt = new TextBox();
	    tbEt.setWidth("100px");
	    tbEt.setReadOnly(true);
	    tbEt.setName("energia");
	    tbEt.setTitle("Energia del blocco");
	    FlexTable hpnl = new FlexTable();
	    hpnl.setWidget(0,0,lb);
	    hpnl.setWidget(0,1,tbEt);
	    
	    t1.setWidget(1, 0, hpnl);
	    hPanel.add(t1);
	  
	    t1 = new FlexTable();
	    
	    lb = new Label("Angolo beta (gradi)");
	    lb.setTitle("Angolo della traiettoria");
	    t1.setWidget(0, 0, lb);
	    tbBeta = new TextBox();
	    tbBeta.addKeyPressHandler(new KeyPressHandler() {
	        public void onKeyPress(KeyPressEvent event) {
	          onlynumbers(event);
	        }
	    });
	    
	    tbBeta.setText("45");
	    
	    tbBeta.setWidth("80px");
	    
	    
	    tbBeta.setTitle("Angolo della traiettoria");
	    tbBeta.setName("betat");
	    //slider.setValue(65);
	    t1.setWidget(0, 1, tbBeta);
	    img = new Image("images/betat.png");
	    img.setTitle("Angolo della traiettoria");
	    t1.setWidget(0, 2, img);
	    
	    lb = new Label("Velocità (m/s)");
	    lb.setTitle("Velocità traslazionale del blocco");
	    t1.setWidget(1, 0, lb);
	    tbVt = new TextBox();
	    tbVt.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				computeEt();
			}
		});
	    tbVt.addKeyPressHandler(new KeyPressHandler() {
	        public void onKeyPress(KeyPressEvent event) {
	          onlynumbers(event);
	        }
	    });
	    tbVt.setWidth("80px");
	    tbVt.setName("vt");
	    tbVt.setTitle("Velocità traslazionale del blocco");
	    
	    t1.setWidget(1, 1, tbVt);
	    img = new Image("images/vt.png");
	    img.setTitle("Velocità traslazionale del blocco");
	    t1.setWidget(1, 2, img);
	    

	    lb = new Label("Spin (rad/s)");
	    lb.setTitle("Velocità angolare in radianti al secondo");
	    t1.setWidget(2, 0, lb);
	    tbOmegat = new TextBox();
	    tbOmegat.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				computeEt();
			}
		});
	    tbOmegat.addKeyPressHandler(new KeyPressHandler() {
	        public void onKeyPress(KeyPressEvent event) {
	          onlynumbers(event);
	        }
	    });
	    tbOmegat.setText("0.0");
	    tbOmegat.setWidth("80px");
	    tbOmegat.setName("omegat");
	    tbOmegat.setTitle("Velocità angolare in radianti al secondo");
	    t1.setWidget(2, 1, tbOmegat);
	    img = new Image("images/omegat.png");
	    img.setTitle("Velocità angolare in radianti al secondo");
	    t1.setWidget(2, 2, img);
	    
	    lb = new Label("Volume blocco (m3)");
	    lb.setTitle("Volume in metri cubi del blocco");
	    t1.setWidget(3, 0, lb);
	    tbVolt = new TextBox();
	    tbVolt.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				computePhit();
				computeEt();
			}
		});
	    tbVolt.addKeyPressHandler(new KeyPressHandler() {
	        public void onKeyPress(KeyPressEvent event) {
	          onlynumbers(event);
	        }
	    });
	    tbVolt.setWidth("80px");
	    tbVolt.setName("volt");
	    tbVolt.setTitle("Volume in metri cubi del blocco");
	    t1.setWidget(3, 1, tbVolt);
	    
	    lb = new Label("Diametro del blocco (m)");
	    lb.setTitle("Diametro del blocco calcolato sulla base del volume");
	    t1.setWidget(4, 0, lb);
	    tbPhit = new TextBox();
	    tbPhit.setWidth("80px");
	    tbPhit.setReadOnly(true);
	    tbPhit.setName("pit");
	    tbPhit.setTitle("Diametro del blocco calcolato sulla base del volume");
	    t1.setWidget(4, 1, tbPhit); 
	    img = new Image("images/phit.png");
	    img.setTitle("Diametro del blocco calcolato sulla base del volume");
	    t1.setWidget(4, 2, img);
	    
	    lb = new Label("Densità blocco (kg/m3)");
	    lb.setTitle("Peso per unità di volume del blocco");
	    t1.setWidget(5, 0, lb);
	    tbRhot = new TextBox();
	    tbRhot.setText("2400");
	    tbRhot.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				computePhit();
				computeEt();
			}
		});
	    tbRhot.addKeyPressHandler(new KeyPressHandler() {
	        public void onKeyPress(KeyPressEvent event) {
	          onlynumbers(event);
	        }
	    });
	    tbRhot.setWidth("80px");
	    tbRhot.setName("rhot");
	    tbRhot.setTitle("Peso per unità di volume del blocco");
	    t1.setWidget(5, 1, tbRhot); 
	    img = new Image("images/rot.png");
	    img.setTitle("Peso per unità di volume del blocco");
	    t1.setWidget(5, 2, img);
	    
	    lb = new Label("Massa blocco (kg)");
	    lb.setTitle("Massa del blocco");
	    t1.setWidget(6, 0, lb);
	    tbMt = new TextBox();
	    tbMt.setReadOnly(true);
	    tbMt.setWidth("80px");
	    tbMt.setName("rmassa");
	    tbMt.setTitle("Massa del blocco");
	    t1.setWidget(6, 1, tbMt); 
	    
		  /*  
	    lb = new Label("Spin (rad/s)");
	    lb.setTitle("Velocità angolare in radianti al secondo");
	    t1.setWidget(7, 0, lb);
	    tbOmegat = new TextBox();
	    tbOmegat.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				computeEt();
			}
		});
	    tbOmegat.addKeyPressHandler(new KeyPressHandler() {
	        public void onKeyPress(KeyPressEvent event) {
	          onlynumbers(event);
	        }
	    });
	    tbOmegat.setWidth("80px");
	    tbOmegat.setName("omegat");
	    tbOmegat.setTitle("Velocità angolare in radianti al secondo");
	    t1.setWidget(7, 1, tbOmegat);
	    img = new Image("images/omegat.png");
	    img.setTitle("Velocità angolare in radianti al secondo");
	    t1.setWidget(7, 2, img);*/
	    hPanel.add(t1);
	    panel.add(hPanel);
	    panel.add(new HTML("<br/><p>Quando hai terminato di inserire i dati per continuare, prego fai " + 
		"click su \"Next.\"</p>"));
	}
	
	protected boolean computeEt()
	{
		String sValue = tbVt.getText();
		speed = Double.parseDouble(sValue);
		energy = 0.5*mass*Math.pow(speed,2.0);
		energy = energy/1000.0;
		long x=(long)(energy*100);
		tbEt.setText(Double.toString(((double)x/100)));
		return true;
	}

	protected boolean computePhit()
	{
		String sValue = tbVolt.getText();
		volume = Double.parseDouble(sValue);
		sValue = tbRhot.getText();
		rho = Double.parseDouble(sValue);
		mass = volume*rho;
		long x=(long)(mass*100);
		tbMt.setText(Double.toString((double)x/100));
		// (6V/pi)1/3
		diameter=Math.pow(6.0*volume/Math.PI,1.0/3.0);
		long y=(long)(diameter*100);
		tbPhit.setText(Double.toString((double)y/100));
		return true;
	}
	
	
	protected boolean onlynumbers(KeyPressEvent event)
	{

		char inputC = event.getCharCode();
		Character chr = new Character(inputC);
		if (Character.isLetter(inputC)) {
			((TextBox) event.getSource()).cancelKey();
		} else if (chr.compareTo(new Character(','))==0) {
			((TextBox) event.getSource()).cancelKey();
		}
		return true;
	}
	
	@Override
	public Widget asWidget() {     
		
		return panel;
	}
	
	@Override
	public PageID getPageID() {
		return PAGEID;
	}

	@Override
	public String getTitle() {
		
		return "Traiettoria del Blocco";
	}

}
